package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.sky.sys.service.SysCommonService;
import org.sky.sys.util.page.PageListData;
import org.sky.util.BeanUtils;
import org.sky.util.CamelUtils;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysCommonController
 * @ProjectName springboot-platform
 * @Description: TODO{通用请求处理}
 * @date 2019/3/8 11:35
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/sys/SysCommon")
public class SysCommonController {
    private static final Logger log = LogUtils.getLogger(SysCommonController.class);
    @Autowired
    private SysCommonService sysCommonService;

    /**
     * 通用下拉选择
     * @param request
     * @return
     */
    @PostMapping(value = "/queryComboBoxData",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String queryComboBoxData(HttpServletRequest request){
        Map param= new HashMap();
        String entity = request.getParameter("entity");
        String fields = request.getParameter("fields");
        String filter = request.getParameter("filter");
        String sort = request.getParameter("sort");
        param.put("table", CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(entity)));
        String columns = "";
        String[] fieldsArray = fields.split(",");
        for(int i=0;i<fieldsArray.length;i++){
            columns = columns+CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(fieldsArray[i]));
            if(i!=fieldsArray.length-1){
                columns=columns+",";
            }
        }
        param.put("fields", columns);
        String filterValue = "";
        if(!StringUtils.isBlank(filter)&&!StringUtils.isBlank(filter)&&!"undefined".equals(filter)){
            Map f= JsonUtils.json2map(filter);
            for (Object key : f.keySet()) {
                String column = CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar((key+"").split("@")[0]));
                filterValue=filterValue+" and "+ column +" "+(key+"").split("@")[1]+" '"+f.get(key)+"'";
            }
        }
        if(!StringUtils.isBlank(filterValue)&&!StringUtils.isBlank(filterValue)){
            param.put("filter", filterValue);
        }
        if(!StringUtils.isBlank(sort)){
            param.put("sort", sort);
        }
        List<Map> list = sysCommonService.queryComboData(param);
        return JsonUtils.obj2json(list);
    }

    /**
     * 下拉表格分页查询
     * @param request
     * @return
     */
    @PostMapping(value = "/queryCombogridDataByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String queryCombogridDataByPage(HttpServletRequest request){
        Map param= new HashMap();
        String entity = request.getParameter("entity");
        String fields = request.getParameter("fields");
        String qField = request.getParameter("qField");
        String filter = request.getParameter("filter");
        String value = request.getParameter("q");
        String sort = request.getParameter("sort");
        String page=request.getParameter("page");//当前第几页
        String rows=request.getParameter("rows");//每页显示条数
        if(StringUtils.isBlank(page)){
            page="1";
        }
        if(StringUtils.isBlank(rows)){
            rows="20";
        }
        int pageNum=Integer.parseInt(page);
        int pageSize=Integer.parseInt(rows);
        param.put("table", CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(entity)));
        String columns = "";
        String[] fieldList = fields.split(",");
        for(int i=0;i<fieldList.length;i++){
            String field = fieldList[i];
            columns = columns+CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(field));
            if(i!=fieldList.length-1){
                columns=columns+",";
            }
        }
        param.put("fields", columns);
        String filterValue="";
        if(!StringUtils.isBlank(qField)&&!StringUtils.isBlank(value)&&!"undefined".equals(value)){
            String[] qs = qField.split(",");
            for(int i=0;i<qs.length;i++) {
                String q = qs[i];
                if(i==0){
                    filterValue = filterValue+" and ( ";
                    filterValue = filterValue + CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(q)) + " like '" + value + "%'";
                }else if(i==qs.length-1){
                    filterValue = filterValue + " or " +CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(q)) + " like '" + value + "%')";
                }else{
                    filterValue = filterValue + " or " +CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar(q)) + " like '" + value + "%'";
                }

            }
        }
        if(!StringUtils.isBlank(filter)&&!StringUtils.isBlank(filter)&&!"undefined".equals(filter)){
            Map f= JsonUtils.json2map(filter);
            for (Object key : f.keySet()) {
                String column = CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar((key+"").split("@")[0]));
                filterValue=filterValue+" and "+ column +" "+(key+"").split("@")[1]+" '"+f.get(key)+"'";
            }
        }
        if(!StringUtils.isBlank(filterValue)&&!StringUtils.isBlank(filterValue)){
            param.put("filter", filterValue);
        }
        if(!StringUtils.isBlank(sort)){
            Map<String,Object> sortMap = JsonUtils.json2map(sort);
            String sortSql = "";
            for (Map.Entry<String, Object> entry : sortMap.entrySet()) {
                sortSql = sortSql +" "
                        + CamelUtils.camelToUnderline(CamelUtils.toLowerCaseFirstChar((entry.getKey())))+" "
                        +entry.getValue()+",";
            }
            param.put("sort", sortSql.substring(0,sortSql.length()-1));
        }
        PageListData pageListData = sysCommonService.selectComboDataByPage(pageNum,pageSize,param);
        String data = JsonUtils.obj2json(pageListData);
        return data;
    }
}
